<template>
  <div class="app-container">
    <!-- 添加或修改设备对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="show"
      @on-cancel="show = false"
      width="1200px"
      @open="onOpen"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="设备名称" prop="deviceName">
          <el-input
            v-model="queryParams.deviceName"
            placeholder="请输入设备名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="imei" prop="imei">
          <el-input
            v-model="queryParams.imei"
            placeholder="请输入imei"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="deviceList"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column
          type="facilityName"
          label="设施"
          width="60"
          align="left"
        />
        <el-table-column
          label="数据时间"
          align="left"
          prop="dataTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.dataTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否在线" align="left" prop="onlineStatus">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.dev_online_status"
              :value="scope.row.onlineStatus"
            />
          </template>
        </el-table-column>
        <el-table-column label="信号强度" align="left" prop="rssi" />
        <el-table-column label="信噪比" align="left" prop="snr" />
        <el-table-column label="电池电压(伏特)" align="left" prop="voltage" />
        <el-table-column label="距离值(厘米)" align="left" prop="distance" />
        <el-table-column label="信号接收功率" align="left" prop="rsrp" />
        <el-table-column label="温度" align="left" prop="temperature" />
        <el-table-column label="信号强度级别" align="left" prop="sign" />
        <el-table-column label="满溢状态" align="left" prop="full">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.full" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="帧计数" align="left" prop="frameCount" /> -->
        <el-table-column label="是否火灾报警" align="left" prop="fire">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.fire" />
          </template>
        </el-table-column>
        <el-table-column label="是否倾倒" align="left" prop="fall">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.fall" />
          </template>
        </el-table-column>
        <el-table-column
          label="电池状态"
          align="left"
          prop="batteryQuantityStatus"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_yes_no"
              :value="scope.row.batteryQuantityStatus"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="left"
          class-name="small-padding fixed-width"
          width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-paperclip"
              @click="handleBind(scope.row)"
              >绑定</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice } from "@/api/base/device";

import { bindDevice } from "@/api/base/facility";

export default {
  name: "Device",
  dicts: ["base_device_type"],
  props: {
    visible: {
      type: Boolean,
      default: function () {
        return false;
      },
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 设备类型字典
      deviceTypeOptions: [],
      enableOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: null,
        deviceType: null,
        bind: 0, //代表未绑定的
        imei: null,
      },
      title: "",
    };
  },
  computed: {
    show: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      this.queryParams.deviceType = this.deviceType;

      listDevice(this.queryParams).then((response) => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /**绑定按钮操作 */
    handleBind(row) {
      bindDevice({
        facilityId: this.facilityObj.facilityId,
        deviceId: row.deviceId,
      }).then((response) => {
        if (response.code === 200) {
          this.$modal.msgSuccess("绑定成功");
          this.getList();
          this.$emit("refreshList");
          this.show = false;
        }
      });
    },
    onOpen() {
      this.title = this.facilityObj.facilityName + "-绑定满溢传感器";
      this.resetQuery();
    },
  },
};
</script>
